package com.db.log.parser;

import java.io.BufferedReader;
import java.io.File;
import java.io.FileInputStream;
import java.io.InputStream;
import java.io.InputStreamReader;
import java.text.DateFormat;
import java.text.SimpleDateFormat;
import java.util.Scanner;
import java.util.regex.Matcher;
import java.util.regex.Pattern;
import java.util.zip.GZIPInputStream;

public class LogParserTest {


	public static void main(String[] args) throws Exception {
		System.out.println("Starting...");
		
//		DateFormat format = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss,SSS");
//		System.out.println(format.);
//		
		
//		String name = "lonindapnp166-ap0155-prod.performance.20110218-083425.log.gz";
//		Pattern pattern = Pattern.compile("([\\p{Alnum}]+)-([\\p{Alnum}]+)-([\\p{Alnum}]+).(performance).[\\p{Digit}-]+.log.gz");
//		
//		Matcher matcher = pattern.matcher(name);
//		if (matcher.find()) {
//			System.out.println("File:			" + matcher.group(0));
//			System.out.println("Host:			" + matcher.group(1));
//			System.out.println("DAP ID:			" + matcher.group(2));
//			System.out.println("Environment:	" + matcher.group(3));
//			System.out.println("Log type:		" + matcher.group(4));
//		}
		

//		parse(
//			new FileInputStream(
//				new File("D:/Projects/logs/3DSF/lonindapnp166-ap0155-prod.performance-exceptions.20110218-194011.log.archive_in_progress")
//			)
//		);
		System.out.println("Finished.");
	}
	
	
	private static void parse(InputStream in) throws Exception {
		
		Pattern linePattern = Pattern.compile(
			"\\[([\\p{Digit}\\p{Blank}:,-]+)\\] \\[([\\p{Alpha}\\p{Blank}]+)\\] \\{([\\p{Digit}]+)\\} \\[([\\p{Graph}\\p{Blank}]+)\\] ([\\p{Alpha}\\p{Blank}]+) ([\\p{Alnum}_.]+\\([^\\)]*\\)) (.+)"
		);
		
		StringBuffer matchedBuffer = new StringBuffer();
		StringBuffer unmatchedBuffer = new StringBuffer();
		
		BufferedReader reader = 
			new BufferedReader(
				new InputStreamReader(in)
			);
		
		String line = null;
		while ((line = reader.readLine()) != null) {
			Matcher matcher = linePattern.matcher(line);
			
			if (matcher.find()) {
				if (unmatchedBuffer.length() > 0) {
					matchedBuffer.append(" ").append(unmatchedBuffer);
					parseBuffer(matchedBuffer, linePattern);
					matchedBuffer = new StringBuffer(line);
					unmatchedBuffer = new StringBuffer();
				}
			}
			
			else {
				if (line.trim().length() > 0) {
					if (unmatchedBuffer.length() > 0) {
						unmatchedBuffer.append(" ");
					}
					unmatchedBuffer.append(line);
				}
				
				matcher = linePattern.matcher(unmatchedBuffer);
				if (matcher.find()) {
					parseBuffer(matcher);
					matchedBuffer = new StringBuffer(unmatchedBuffer);
					unmatchedBuffer = new StringBuffer();
				}
			}
			
		}
	}
	
	
	private static void parseBuffer(StringBuffer buffer, Pattern pattern) {
		parseBuffer(pattern.matcher(buffer));
	}
		
	
	private static void parseBuffer(Matcher matcher) {
		if (matcher.find()) {
			System.out.println("Date:		" + matcher.group(1));
			System.out.println("Log Level:	" + matcher.group(2));
			System.out.println("Execution:	" + matcher.group(3));
			System.out.println("Username:	" + matcher.group(4));
			System.out.println("State:		" + matcher.group(5));
			System.out.println("Method:		" + matcher.group(6));
			System.out.println("Raw text:	" + matcher.group(7));
			System.out.println("____________________________________________________________________________________________________\n");
		}
	}
	

	
	/* 
	 
	\\[([\\p{Digit}\\p{Blank}:,-]{10,13})\\] 
	\\[([\\p{Alpha}\\p{Blank}]+)\\] 
	\\{([\\p{Digit}]+)\\} 
	\\[([\\p{Graph}\\p{Blank}]+)\\] 
	([\\p{Alpha}\\p{Blank}]+) 
	([\\p{Alnum}_.]+\\([^\\)]*\\)) (.+)

	 */
	
	/*
	 [2011-02-18 09:55:13,019] [ERROR] {1491074} [<anonymous>] exception gm.server.xstatic.impl.service.staticdata.CreditCurveServiceBean.getCreditCurveByName(java.lang.String)  5ms  arguments:([CDX9_0_100_2Y])
gm.server.xstatic.boca.service.staticdata.CreditCurveNotFoundException: Cannot find credit curve, name = CDX9_0_100_2Y

	at gm.server.xstatic.impl.service.staticdata.CreditCurveServiceBean.getCreditCurveByName_aroundBody6(CreditCurveServiceBean.java:211)
	at gm.server.xstatic.impl.service.staticdata.CreditCurveServiceBean$AjcClosure7.run(CreditCurveServiceBean.java:1)
	at org.aspectj.runtime.reflect.JoinPointImpl.proceed(JoinPointImpl.java:221)
	at gm.server.xstatic.impl.aspects.LoggerAspect.aroundLog(LoggerAspect.java:72)
	at gm.server.xstatic.impl.service.staticdata.CreditCurveServiceBean.getCreditCurveByName(CreditCurveServiceBean.java:203)
	at gm.server.xstatic.impl.service.staticdata.CreditCurveServiceBean_oavhcy_EOImpl.getCreditCurveByName(CreditCurveServiceBean_oavhcy_EOImpl.java:2283)
	at gm.server.xstatic.impl.service.staticdata.CreditCurveServiceBean_oavhcy_EOImpl_WLSkel.invoke(Unknown Source)
	at weblogic.rmi.internal.BasicServerRef.invoke(BasicServerRef.java:589)
	at weblogic.rmi.cluster.ClusterableServerRef.invoke(ClusterableServerRef.java:230)
	at weblogic.rmi.internal.BasicServerRef$1.run(BasicServerRef.java:477)
	at weblogic.security.acl.internal.AuthenticatedSubject.doAs(AuthenticatedSubject.java:363)
	at weblogic.security.service.SecurityManager.runAs(SecurityManager.java:147)
	at weblogic.rmi.internal.BasicServerRef.handleRequest(BasicServerRef.java:473)
	at weblogic.rmi.internal.wls.WLSExecuteRequest.run(WLSExecuteRequest.java:118)
	at weblogic.work.ExecuteThread.execute(ExecuteThread.java:201)
	at weblogic.work.ExecuteThread.run(ExecuteThread.java:173)
	 */

}
